<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>哈希函数的实现</title>
</head>
<body>
    <script>
        function hashFunc(str,size) {  
            let hashFunc = 0;

            for (let i = 0; i < str.length; i++) {
                //有一个尽量不会重复的hashFunc值
                hashFunc = 37 * hashFunc + str.charCodeAt(i);
            }
            //取模后,下标值范围在0-size之间,最大不会等于size
            return hashFunc % size;
        }

        //测试hash函数
        console.log(hashFunc('abc',7));
        console.log(hashFunc('nbc',7));
        console.log(hashFunc('dbc',7));
        console.log(hashFunc('rtc',7));
    </script>
</body>
</html>